var amqp = require('amqplib/callback_api');

var args = process.argv.slice(2);
if (args.length != 2) {
    console.log("Usage: pma_sendRufundMessage.js <OutTradeNo> <Money>");
    process.exit(1);
}

var outTradeNo = args[0];
var money = args[1];

amqp.connect('amqp://Arrcen:arrcen911@59.110.172.150:5672/', function (err, conn) {
    if (err) {exit(err);}
    conn.createChannel(function (err, ch) {
        if (err) { exit(err); }

        var refundMessage = {
            OutTradeNo: outTradeNo,
            Money: money
        };

        var ex = 'Arrcen.EventMessageExchange.ChanJoy.Messaging.Model.HisMessage.RefundMessage';

        ch.assertExchange(ex, 'fanout', { durable: false });

        ch.publish(ex, '', new Buffer(JSON.stringify(refundMessage)));
        console.log("[x] Send ", refundMessage);
    });

    setTimeout(() => {
        conn.close();
        process.exit(0);
    }, 500);
});

function exit(err) {
    console.log(err);
    process.exit(1);
}